package kr.hkit.androidcomplete03.SQLite;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class sqliteTest1 extends Activity {
	MyDBHelper myHelper;
	EditText edtName, edtNumber;
	TextView edtNameResult, edtNumberResult;
	Cursor cursor;
	Button btnInit, btnInsert, btnSelect;
	SQLiteDatabase sqlDB;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqlitetest1);
		
		edtName = (EditText) findViewById(R.id.edtname);
		edtNumber = (EditText) findViewById(R.id.edtnum);
		
		edtNameResult = (TextView) findViewById(R.id.edtnameresult);
		edtNumberResult = (TextView) findViewById(R.id.edtnumberresult);
		
		btnInit = (Button) findViewById(R.id.btninit);
		btnInsert = (Button) findViewById(R.id.btninsert);
		btnSelect = (Button) findViewById(R.id.btnselect);
			
	}
	
	public void mOnClick(View v){
		myHelper = new MyDBHelper(this);
		
		switch(v.getId()){
		case R.id.btninit:
			sqlDB = myHelper.getWritableDatabase();
			myHelper.onUpgrade(sqlDB, 1, 2);
			sqlDB.close();
			break;
			
		case R.id.btninsert:
			sqlDB = myHelper.getWritableDatabase();
			
			sqlDB.execSQL("insert into groupTBL values (null, '" + edtName.getText().toString() + "','" + edtNumber.getText().toString() + "');");
			sqlDB.close();
			Toast.makeText(getApplicationContext(), "입력완료", Toast.LENGTH_SHORT).show();
			
			break;
			
		case R.id.btnselect:
			sqlDB = myHelper.getReadableDatabase();
			cursor = sqlDB.rawQuery("select * from groupTBL;", null);
			
			String strName = "그룹이름" + "\r\n" + "-------" + "\r\n";
			String strNumber = "인원" + "\r\n" + "-------" + "\r\n";
			
			while(cursor.moveToNext()){
				strName += cursor.getString(0) + "\r\n";
				strNumber += cursor.getString(1) + "\r\n";
			}
			
			edtNameResult.setText(strName);
			edtNumberResult.setText(strNumber);
			
			cursor.close();
			sqlDB.close();
			
			break;
		
		}
	}
}
